﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace SPHelper
{
	public class Validation
	{
		public static bool IsValidUrl(string url)
		{
			bool isValid = false;
			try
			{
				if (string.IsNullOrEmpty(url)) { throw new Exception("url passed is null or empty"); }
				if (url.Length < 7 || url.ToLower().Substring(0,7) != "http://") { throw new Exception("Url must start with \"http://\""); }

				// otherwise, assume valid
				isValid = true;
			}
			catch
			{
				isValid = false;
			}
			return isValid;
		}

		/// <summary>
		/// determine if passed email address is valid
		/// Make sure to trim email before passing
		/// </summary>
		/// <param name="email"></param>
		/// <returns></returns>
		public static bool IsValidEmail(string email)
		{
			bool isValid = false;
			if (!string.IsNullOrEmpty(email))
			{
				if (!email.Contains(" "))
				{
					var regex = new Regex(@"[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", RegexOptions.IgnoreCase);
					isValid = regex.IsMatch(email);
				}
			}
			return isValid;
		}
	}
}
